import {Toast} from 'vant'
const NOTICE_SERVER = "ws://139.186.158.104:81/music/ws";
var ws;
var stateobj;
var tip = "";
var xobj = null;
var mtype = 0;
var Hostid = null;
var lastsendtime = null;
var change_callback = function () { }
var recv_callback = function () { }
var gs = function () { return (new Date()).getTime(); }
t.stool = {
    stateData:{
        tip:"",
        joinId:0,
    },
    init: function () { //绑定事件
        // $(obj).find("#sync_create_room").click(t.stool.createHost);
        // $(obj).find("#sync_join_room").click(t.stool.join);
        // $(obj).find("#sync_stop").click(t.stool.close);
        // stateobj = $(obj).find("#sync_state");
        // xobj = obj;
        setInterval(t.stool.ivl, 100);

        tip = "准备就绪,可以连接";
    },
    createHost: function () {
        if (mtype > 0) { Toast("请先退出房间"); return; }
        mtype = 1;
        with (t.stool) {
            createws(function () {
                if (Hostid == null && (Hostid = localStorage.getItem("stoolid")) == null) {
                    Hostid = "1" + parseInt(Math.random() * 10000).toString()
                    localStorage.setItem("stoolid", Hostid)
                }
                send({ type: "A", id: Hostid.toString() })
            });
        }
    },
    join: function () {
        if (mtype == 1) { Toast("请先退出房间"); return; }
        mtype = 2;
        with (t.stool) {
            createws(function () {
                send({ type: "B", id: this.joinid });
            });
        }
    },
    close: function () { //关闭
        if (mtype == 1) {
            t.stool.send({ type: "D", id: Hostid.toString() })
        }
        ws.close();
    },
    send: function (args) {
        if (!t.stool.state) return false;
        lastsendtime = gs();
        return ws.send(typeof args != 'string' ? JSON.stringify(args) : args);
    },
    createws: function (fn) {
        ws = new WebSocket(NOTICE_SERVER);
        tip = "连接服务器中";
        ws.onopen = function () {
            // Web Socket 已连接上，使用 send() 方法发送数据
            change_callback(mtype);
            tip = "连接成功";
            fn();
        };
        ws.onmessage = function (evt) {
            var data = evt.data;
            jn = JSON.parse(data);

            data = jn['data'];
            switch (jn["type"]) {
                case "M":
                    tip = "房间ID:" + Hostid;
                    break;
                case "C": //消息
                    tip = "同步中";
                    recv_callback(data);
                    break;
                case "P":
                    tip = "暂无消息";
                    break;
                case "T":
                    break;
                default:
                    console.log(jn)
                    tip = jn['msg'];
            }
        }
        ws.onclose = function (e) {
            console.log('websocket 断开: ' + e.code + ' ' + e.reason + ' ' + e.wasClean)
            mtype = 0;
            change_callback(mtype);
            tip = "连接已断开";
        }

    },
    ivl: function () {
        if (lastsendtime + 10 * 1000 < gs()) {
            t.stool.send({ "type": "P" })
        }
        this.stateData.tip=tip;
        // $("#stateobj").html(tip);
    },
    get state() {
        if (!(ws && ws.readyState == ws.OPEN)) return false;
        return true;
    },
    senddata: function (data) {
        t.stool.send({ type: "C", id: Hostid, data: data })
    },
    onchange: function (fn) {
        change_callback = fn;
    },
    recv: function (fn) {
        recv_callback = fn;
    },
    get wsInstance() {
        return ws;
    },
    get joinid() {
        return this.stateData.joinId;
    }
}

export default {SyncPlay:t}